package com.jdone.compus.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.dto.BlowUserInfo;
import com.jdone.compus.utils.AESJSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;

@RestController
@RequestMapping("/blow/login")
@Api(value = "吹风账户信息还原", tags = "吹风账户信息还原")
public class BlowXueXinController {


    @Value("${aes.keyStr}")
    private String keyStr;

    @Value("${aes.keyIv}")
    private String keyIv;

    @ApiOperation("学信免密登录接口")
    @GetMapping("/authenticate")
    public ApiResponse authenticate(@RequestParam String query) {
        try {
            // 1. 解密账户加密串
            String decryptedData = AESJSUtils.decrypt1(query, keyStr, keyIv);

            // 2. 将解密后的字符串解析为JSON对象
            ObjectMapper objectMapper = new ObjectMapper();
            BlowUserInfo accountInfo = objectMapper.readValue(decryptedData, BlowUserInfo.class);

            // 3. 返回解密后的账户信息
            return new ApiResponse(200, true, "登录成功", accountInfo);
        } catch (Exception e) {
            // 发生错误时返回错误信息
            return new ApiResponse(500, false, "登录失败: " + e.getMessage(), null);
        }
    }
}
